package top.braycep.utils;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileUtils {
    private static final String dir = System.getProperty("user.home");
    public static String sep;

    static {
        sep = System.getProperty("file.separator");
        if (StringUtils.isEmptyOrBlank(sep)) {
            if (dir.contains("\\")) {
                sep = "\\";
            } else {
                sep = "/";
            }
        }
    }

    public static File chooseDir(Component mainUI, String defaultDir) {
        JFileChooser fileChooser = new JFileChooser();
        File curDir = new File(StringUtils.isEmptyOrBlank(defaultDir) ? dir : defaultDir);
        fileChooser.setCurrentDirectory(curDir);
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fileChooser.setMultiSelectionEnabled(false);
        fileChooser.showDialog(mainUI, "请选择文件夹");
        return fileChooser.getSelectedFile();
    }

    public static File chooseFile(Component mainUI, String defaultDir) {
        JFileChooser fileChooser = new JFileChooser();
        File curDir = new File(StringUtils.isEmptyOrBlank(defaultDir) ? dir : defaultDir);
        fileChooser.setCurrentDirectory(curDir);
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fileChooser.setMultiSelectionEnabled(false);
        fileChooser.showDialog(mainUI, "请选择文件");
        return fileChooser.getSelectedFile();
    }

    public static File[] chooseFiles(Component mainUI, String defaultDir) {
        JFileChooser fileChooser = new JFileChooser();
        File curDir = new File(StringUtils.isEmptyOrBlank(defaultDir) ? dir : defaultDir);
        fileChooser.setCurrentDirectory(curDir);
        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        fileChooser.setMultiSelectionEnabled(true);
        fileChooser.showDialog(mainUI, "请选择文件(夹)");
        return fileChooser.getSelectedFiles();
    }

    public static String analysisPath(String path) {
        if (StringUtils.isEmptyOrBlank(path)) {
            return "";
        }

        if (isFile(path)) {
            path = new File(path).getParent();
        }

        String regex = "[/\\\\](com|cn|xyz|net|top|io)[/\\\\]?.*";
        Pattern p = Pattern.compile(regex);
        Matcher matcher = p.matcher(path);
        if (matcher.find()) {
            String s = matcher.group();
            if (StringUtils.isNotEmptyOrBlank(s)) {
                s = s.replace(sep + sep, ".").replace(sep, ".");
                if (s.startsWith(".")) {
                    s = s.substring(1);
                }
                if (s.endsWith(".")) {
                    s = s.substring(0, s.length() - 1);
                }

                return s;
            }
        }

        return "";
    }

    public static boolean isFile(String path) {
        if (StringUtils.isEmptyOrBlank(path)) {
            return false;
        }

        File file = new File(path);
        return file.exists() && file.isFile();
    }
}
